﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public partial class TSVV08
    {
        #region ==IsOneMore===========
        public static readonly DependencyProperty IsOneMoreProperty = DependencyProperty.Register("IsOneMore",
            typeof(bool), typeof(TSVV08),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsOneMoreChanged));
        public bool IsOneMore
        {
            get
            {
                return (bool)GetValue(IsOneMoreProperty);
            }
            set
            {
                SetValue(IsOneMoreProperty, value);
                if (MV == null)
                    return;
                ((VVVLIEF)MV).IsOneMore = value;
            }
        }
        private static void OnIsOneMoreChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSVV08;
            if (src != null)
            {
                src.IsOneMore = (bool)e.NewValue;
            }
        }

        #endregion

        #region ==VVT===========
        public static readonly DependencyProperty VVTProperty = DependencyProperty.Register("VVT",
            typeof(string), typeof(TSVV08),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVVTChanged));
        public string VVT
        {
            get
            {
                return (string)GetValue(VVTProperty);
            }
            set
            {
                SetValue(VVTProperty, value);
            }
        }
        private static void OnVVTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSVV08;
            if (src != null)
            {
                src.VVT = (string)e.NewValue;
            }
        }
        #endregion

        #region ==VKPVVTS===========
        public static readonly DependencyProperty VKPVVTSProperty = DependencyProperty.Register("VKPVVTS",
            typeof(string), typeof(TSVV08),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVKPVVTSChanged));
        public string VKPVVTS
        {
            get
            {
                return (string)GetValue(VKPVVTSProperty);
            }
            set
            {
                SetValue(VKPVVTSProperty, value);
                if (MV == null)
                    return;
                ((VVVLIEF)MV).VKPVVTS = value;
            }
        }
        private static void OnVKPVVTSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSVV08;
            if (src != null)
            {
                src.VKPVVTS = (string)e.NewValue;
            }
        }
        #endregion

        public TSVV08()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVVLIEF();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KEY = ""; 
            }
            else
            {
                KEY = dwMaster["VVLIEFSCH"].ToString(); 
            }
        }
        public override void OnVKPChg(string vkp)
        {
            base.OnVKPChg(vkp);
            if (vkp.Length < 12) return;
            if (TBMain.Rows.Count > 0)
            {
                if (TBMain.Rows[0]["VVTAUFTYP"].ToString() + TBMain.Rows[0]["VVTAUFKRS"].ToString() + TBMain.Rows[0]["VVTAUFLFD"].ToString() == vkp.Substring(0, 11))
                    return;
            }
            TBMain.Clear();           
            dwData = VVVTLIEF.SVVT.TBMain.NewRow();
            dwData["VVTMANDANT"] = app.Mandant;
            dwData["VVTAUFTYP"] = vkp.Substring(0, 1);
            dwData["VVTAUFKRS"] = vkp.Substring(1, 2);
            dwData["VVTAUFLFD"] = vkp.Substring(3, 6);
            //该销售单所有的发货单
            SResult = ((VVVLIEF)MV).QueryVVLIEF(dwData, 8);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string liefsch = row["VVLIEFSCH"].ToString();
                row["KEY"] = Helpers.Helper.GetSubRollNum(liefsch).ToString();
            }
            MergeData(SResult, false);
        }
        /// <summary>
        /// 1.装载销售单的所有发货单
        /// 2.如果该销售单行存在多个发货单，定位到最后一个
        /// 3.发货单号码选择的改变，仅仅用于加入发货单，不再引起销售单的各种变化
        /// 4.在VVTLIEF中增加改行对应的列表，通过这个选择来进行接下来的判断
        /// </summary>
        public override void LOADBYAUF()
        {
            //LOADBYVK(ORDERNUM);//装载整个销售单的VV
            return;
            if (ORDERNUM.Length < 20)
                return;
            string liefsch = "";
            liefsch=ORDERNUM.Substring(11, 8);
            if (liefsch.Trim().Length > 0)
            {
                Locate("VVLIEFSCH", liefsch);//是否可以直接定位到这个请求的发货单
                if (dwMaster != null)
                    return;
            }
            dwData = VVVTLIEF.SVVT.TBMain.NewRow();
            dwData["VVTMANDANT"] = app.Mandant;
            dwData["VVTAUFTYP"] = ORDERNUM.Substring(19, 1);
            dwData["VVTAUFKRS"] = ORDERNUM.Substring(0, 2);
            dwData["VVTAUFLFD"] = ORDERNUM.Substring(2, 6);            
            dwData["VVTAUFPOS"] = ORDERNUM.Substring(8, 3);
            //销售单行对应的发货单
            SResult = VVVTLIEF.SVVT.QueryVVTLIEF(dwData, 10);
            int count = 0;
            VKPVVTS = "";
            string mlief = "";
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                liefsch = row["VVTLIEFSCH"].ToString();
                if (count == 0)
                {
                    mlief = liefsch;
                }
                count++;
                VKPVVTS += Helpers.Helper.GetSubRollNum(liefsch).ToString() + ";";
            }
            //if (count > 1)
            IsOneMore = true;
            Locate("VVLIEFSCH", mlief);
            base.LOADBYAUF();
        }
    }
}